package com.hzw.saas.web.admin.job.task;

import com.hzw.saas.api.hpc.IHpcAccountRecordService;
import com.hzw.saas.api.hpc.IHpcAccountUserService;
import com.hzw.saas.api.hpc.bo.HpcAccountRecordBo;
import com.hzw.saas.api.hpc.bo.HpcAccountUserBo;
import com.hzw.saas.api.hpc.enums.HpcAccountStatus;
import com.hzw.saas.api.hpc.query.HpcAccountUserFrozenQuery;
import com.hzw.saas.common.quartz.model.ScheduleJob;
import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * HPC账号冻结JOB
 * @author sonam
 * @sine 2021/12/2 11:37 上午
 */
@Slf4j
@RequiredArgsConstructor
@Service("hpcAccountFreezeTask")
public class HpcAccountFreezeTask {

    private final IHpcAccountUserService accountUserService;
    private final IHpcAccountRecordService hpcAccountRecordService;

     /**
     * 冻结所有无效的HPC账号
     * @param scheduleJob
     */
    @SneakyThrows
    public void freeze(ScheduleJob scheduleJob) {
        List<HpcAccountRecordBo> invalidHpcAccounts = hpcAccountRecordService.getInvalidHpcAccount();
        log.info("查询到无效的HPC账号数量: {}", invalidHpcAccounts.size());
        invalidHpcAccounts.forEach(invalidHpcAccount -> {
            String hpcAccountRecordBoPid = invalidHpcAccount.getPid();
            HpcAccountUserBo hpcAccountUserBo = accountUserService.getMainUserByRecordId(hpcAccountRecordBoPid);
            if(Objects.isNull(hpcAccountUserBo)) {
                return;
            }
            if(Objects.equals(hpcAccountUserBo.getStatus(), HpcAccountStatus.DISABLED.value())) {
                return;
            }
            HpcAccountUserFrozenQuery frozenQuery = new HpcAccountUserFrozenQuery();
            frozenQuery.setHpcAccountUserId(hpcAccountUserBo.getPid());
            frozenQuery.setHpcAccountStatus(HpcAccountStatus.DISABLED);
            log.info("冻结账号>>>{}<<<", hpcAccountUserBo.getPid());
            accountUserService.frozenHpcAccount(frozenQuery);
        });
    }


}
